﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace SMS.Common.Silverlight
{
    public class ServiceProxy<T>
    {
        private Uri _baseUri;
        private EndpointAddress _endpointAddress;
        private BasicHttpBinding _binding;
        private T _client;

        public ServiceProxy(String serviceUri)
        {
            _baseUri = new Uri
                (Application.Current.Host.Source.ToString().Substring(0, Application.Current.Host.Source.ToString().IndexOf("ClientBin")));
            //("http://localhost/GacOil");
            _endpointAddress = new EndpointAddress(new Uri(_baseUri, serviceUri), null);

            HttpTransportBindingElement transport = new HttpTransportBindingElement();
            transport.MaxBufferSize = int.MaxValue;
            transport.MaxReceivedMessageSize = int.MaxValue;
            transport.TransferMode = TransferMode.Buffered;


            _binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            _binding.MaxBufferSize = int.MaxValue;
            _binding.MaxReceivedMessageSize = int.MaxValue;
            _binding.TransferMode = TransferMode.Buffered;
            _binding.OpenTimeout = _binding.ReceiveTimeout = _binding.SendTimeout = _binding.CloseTimeout = new TimeSpan(0, 5, 0);

            _client = (T)Activator.CreateInstance(typeof(T), new object[] { _binding, _endpointAddress });
        }

        public T Client
        {
            get
            {
                return _client;

            }
        }
    }
}
